package ltd.jdsoft.cute.prometheus.spi.impl;

import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.prometheus.client.Gauge;
import ltd.jdsoft.cute.prometheus.MonitorChain;
import ltd.jdsoft.cute.prometheus.spi.Monitor;

/**
 * 当前接口调用并发数监控
 * 
 * @author zhengzhq
 *
 */
public class ConcurrencyStatus implements Monitor {

  private static Logger logger = LoggerFactory.getLogger(ConcurrencyStatus.class);
  private static Gauge concurrencyGauge =
      Gauge.build().name("concurrency").help("接口当前并发调用数").labelNames(LABEL_NAMES).register();

  @Override
  public Object process(ProceedingJoinPoint joinPoint, MonitorChain chain, String... labelValues)
      throws Throwable {
    logger.debug("start monitor concurrency status...");
    try {
      concurrencyGauge.labels(labelValues).inc();
      return chain.doChain(joinPoint, chain);
    } finally {
      concurrencyGauge.labels(labelValues).dec();
      logger.debug("end monitor concurrency status...");
    }
  }
}
